package com.algorithm.linkedIsRing;

import com.algorithm.Common.LinkedNode;
import com.algorithm.Common.LinkedUtils;

public class AlMain {
    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,4,5,6,7,8,9,10};
        LinkedNode head = LinkedUtils.create(arr);
        LinkedNode p1 = head;
        //下面四行去掉就是链表无环，输出值就是false
        while (p1.getNext() != null) {
            p1 = p1.getNext();
        }
        p1.setNext(head);
        System.out.println(isRing(head));
    }

    /**
     * 判断是否为环
     *
     * @param head 头
     * @return boolean
     */
    public static boolean isRing(LinkedNode head) {
        LinkedNode fast = head;
        LinkedNode slow = head;
        while (fast.getNext() != null && fast.getNext().getNext() != null ) {
            fast = fast.getNext().getNext();
            if(slow == fast){
                return true;
            }
            slow = slow.getNext();
        }
        return false;
    }
}
